//
//  Memory5ViewController.m
//  Memory5
//
//  Created by Jin Hu on 2009/12/10.
//  Copyright Philips Research 2009. All rights reserved.
//
#import "Card.h"
#import "Memory5ViewController.h"
#import <AudioToolbox/AudioServices.h>
#import "MemoryView.h"
#import "State.h"
#import "MemoryStage.h"
@implementation Memory5ViewController

- (void)loadView {
	state = S_TITLE;
	frame = [[UIScreen mainScreen] applicationFrame] ;
	self.view =[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] ] ;
	memoryStage = [[MemoryStage alloc] init];

	themeSong = [[MediaPlayer alloc] initWithPath:[[NSBundle mainBundle] pathForResource:@"greecestage" ofType:@"caf"]];
	[themeSong setRepeat:YES];
	
	stageSong = [[MediaPlayer alloc] initWithPath:[[NSBundle mainBundle] pathForResource:@"polandstage" ofType:@"caf"]];
	[stageSong setRepeat:YES];
	currentSong =themeSong;
	

}


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}
- (void) onTransition {
	if (transitionScreen == nil) {
		transitionScreen = [TransitionScreen alloc];
	}
	if(currentScreen !=transitionScreen){
		[transitionScreen initWithFrame:frame];
		transitionScreen.lastStage=[memoryStage getBackground];
		memoryStage.currentStage+=1;
		transitionScreen.nextStage=[memoryStage getBackground];
		transitionScreen.image=[UIImage imageNamed:@"screen_transition.png"];
		currentScreen = transitionScreen;
		state = S_TRANSITION;
		currentSong= stageSong;
	}
}

- (void) onPlay {
	if (playScreen == nil) {
		playScreen = [MemoryView alloc];
	}
	if(currentScreen !=playScreen){
		[playScreen initWithFrame:[[UIScreen mainScreen] applicationFrame] stage:memoryStage];
		currentScreen = playScreen;
		state = S_PLAY;
		currentSong= stageSong;
	}
 
}


- (void) onEnd {
	if (endScreen == nil) {
		endScreen = [EndScreen alloc];
	}
	if(currentScreen !=endScreen){
		[endScreen initWithFrame:frame];
		//endScreen.player= avatarScreen.avatar;
		//[endScreen.player move:80 y:-10];
		
		currentScreen = endScreen;
		currentSong= themeSong;
	}
}
- (void) onTitle{
	if (titleScreen == nil) {
		titleScreen = [TitleScreen alloc];
	}
	if(titleScreen!=currentScreen){
		[titleScreen initWithFrame:frame];
		memoryStage.currentStage=0;
		currentScreen = titleScreen;
		titleScreen.image = [UIImage imageNamed:@"memory_title.png"];
		currentSong= themeSong;
	}
}
- (void) stateMachine{
	MediaPlayer* oldSong = currentSong;
	Screen* oldScreen = currentScreen;
	switch (state){
		case S_PLAY: 
			[self onPlay];
			break;
		case S_END: 
			[self onEnd];
			break;
		case S_TRANSITION: 
			[self onTransition];
			break;
		case S_TITLE: 
		default: 
			[self onTitle];				
			break;
	}
	state = [currentScreen stateMachine];
	if(oldSong != currentSong){
		[oldSong pause];
		[currentSong play];
	}
	if (oldScreen!=currentScreen) {
		[oldScreen removeFromSuperview];
		[currentScreen retain];
		[self.view addSubview:currentScreen];
	}
	
}

@end
